<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <el-select v-model="listQuery.cateid" placeholder="文章分类" clearable style="width: 150px" class="filter-item">
        <el-option v-for="(item,index) in catelist" :key="index" :label="item.title" :value="item.id" v-html="item.title_show" />
      </el-select>

      <!-- <el-select v-model="listQuery.type" placeholder="Type" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select> -->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        搜索
      </el-button>
      <el-button v-waves v-permission="['/article/addArticle']" class="filter-item" type="primary" @click="add()">
        <i class="el-icon-plus" />&nbsp;&nbsp;添加文章
      </el-button>
    </div>



    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.Id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章分类" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.CateName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章标题" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.Title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章排序" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.Sort }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.Create_times }}</span>
        </template>
      </el-table-column>

      <el-table-column v-permission="['/article/getArticle','/article/updateArticle', '/article/deleteArticle']" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <div>
            <span v-permission="['/article/getArticle']">
              <el-button size="small" type="primary" @click="showArticle(scope.row.Id)">
                查看
              </el-button>
            </span>
            <span v-permission="['/article/updateArticle']">
              <el-button size="small" type="primary" @click="edit(scope.row)">
                  编辑
                </el-button>

              <!-- <router-link :to="'editArticle/'+scope.row.Id">
                <el-button type="primary" size="small">
                  编辑
                </el-button>
              </router-link> -->

            </span>
            <span v-permission="['/article/deleteArticle']">
              <el-button size="small" type="danger" @click="del(scope.row)">
                删除
              </el-button>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="文章详情" :visible.sync="showArticleVisible">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple-dark">
            <h3 style="text-align:center">{{ showList.Title }}</h3>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple-dark">
            <span v-html="showList.Content" />
          </div>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { articleList, getArticle, deleteArticle,getCate } from '@/api/article'
import waves from '@/directive/waves'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import permission from '@/directive/permission/index.js'

export default {
  components: { Pagination },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        keywords:'',
        cateid:''
      },

      showArticleVisible: false,
      showList: {
        Title: '',
        Content: ''
      },

      catelist:null

    }
  },
  created() {
    this.getList()
    this.getCateList()
  },

  methods: {
    getList() {
      this.listLoading = true
      articleList(this.listQuery).then(res => {
        this.list = res.data.data
        this.total = res.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 0.3 * 1000)
      })
    },

    add() {
      this.$router.push({ name: 'addArticle' })
    },


    edit(row){
      this.$router.push({ name: 'updateArticle' ,params:{Id:row.Id}})
    },


    showArticle(row) {
      this.showArticleVisible = true
      getArticle({id:row}).then(res => {
        if (res.code == 20000) {
          this.showList = res.data
        } else {
          this.showList.Title = ''
          this.showList.Content = ''
        }
      })
    },
    // 删除分类
    del(row) {
      this.$confirm('确认删除该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteArticle(row.Id).then(res => {
          this.getList()
          this.$message({
            message: '文章已删除',
            type: 'success'
          })
        })
      }).catch(() => {

      })
    },

    
    //获取分类
    getCateList() {
      this.loading = true
      getCate(this.listQuery).then(res => {
        if (res.code == 20000) {
          this.catelist = res.data
          setTimeout(() => {
            this.loading = false
          }, 0.3 * 1000)
        }
      })
    },

  }
}
</script>

